<?php
/*
 * This class implements the view component of the web app.
 * Currently the only supported template engine is Smarty.
 */
class view
{
    private $smarty = null;

    public function __construct($app)
    {
        if (empty($app))
            throw new Exception('sbook/view: empty app for view!');

        $smarty_path = glob(EXTERNAL."Smarty-*"); 
        if (empty($smarty_path))
            trigger_error('sbook::cannot find smarty!',E_USER_ERROR);
        define('SMARTY_DIR',$smarty_path[0].DS.'libs'.DS);
        require_once(SMARTY_DIR.'Smarty.class.php');

        $path = APPS.$app.DS."views".DS;

        // initialize Smarty
        $this->smarty = new Smarty();
        $this->smarty->template_dir  = $path;
        $this->smarty->compile_dir   = $path.'compiled';
        $this->smarty->config_dir    = $path.'configs';
        $this->smarty->cache_dir     = $path.'cache';
        $this->smarty->plugins_dir[] = $path.'plugins';
        $this->smarty->plugins_dir[] = APPS.'sbook'.DS.'views'.DS.'plugins';
        $this->smarty->debugging     = true;
    }

    public function display($template,$vars = array())
    {
        foreach ($vars as $key => $value)
        {
            $this->smarty->assign_by_ref($key,$value);
        }
        $this->smarty->display($template.".tpl");
    }
}
?>
